package com.acwing.partition36;

import java.io.*;
import java.util.PriorityQueue;

/**
 * @author rkc
 * @date 2022/7/17 10:37
 */
public class AC3531哈夫曼树 {

    private static final int N = 1010;
    private static PriorityQueue<Integer> heap = new PriorityQueue<>(N);
    private static int n;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 0; i < n; i++) heap.add(nextInt());
        int ans = 0;
        while (heap.size() > 1) {
            int q = heap.poll(), p = heap.poll();
            ans += q + p;
            heap.add(q + p);
        }
        writer.println(ans);
        writer.flush();
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
